<?php

namespace App\Admin\Forms;

use App\Models\Type;
use App\Models\Video;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class Gather extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '影视采集';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
       $param = $request->all();
       $Collect = new \Collect($param['url']);
       $data = $Collect->collect();
       $model = new Video();
       $model->name = $data['name'];
       $model->type_id = $param['type_id'];
       $model->type_name = $data['type_name'];
       $model->director = $data['director'];
       $model->performer = $data['performer'];
       $model->desc = $data['desc'];
       $model->url = $data['url'];
       $model->img = $data['img'];
       $res = $model->save($data);
       if ($res){
           admin_success('采集成功');
       }else{
           admin_error('采集失败');
       }

        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->text('url','采集地址')->rules('required');
        $this->select('type_id','影视分类')->rules('required')->options($this->type(1));

    }
    /**
     * 获取影视分类
     * @return array
     */
    public function type($parent)
    {
        $data = Type::where('parent_id',$parent)->select('id','name')->get()->toArray();
        $_id = [];
        $_name = [];
        foreach ($data as $key=>$value){
            array_push($_id,$data[$key]['id']);
            array_push($_name,$data[$key]['name']);

        }
        return array_combine($_id,$_name);
    }
}
